package com.smart.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.List;

import com.smart.system.domain.dto.SensorDataWrapperDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.smart.system.mapper.GasRecordMapper;
import com.smart.system.domain.GasRecord;
import com.smart.system.service.IGasRecordService;

/**
 * 气体记录Service业务层处理
 *
 * @author smart
 * @date 2025-09-18
 */
@Service
public class GasRecordServiceImpl extends ServiceImpl<GasRecordMapper, GasRecord> implements IGasRecordService {

    @Override
    public Boolean add(SensorDataWrapperDto dto) {
        SensorDataWrapperDto.SensorData sensorData = dto.getSensor_data();
        GasRecord record = GasRecord.builder()
                .temperatureValue(String.valueOf(sensorData.getTemperature().getValue()))
                .temperatureUnit(sensorData.getTemperature().getUnit())
                .humidityValue(String.valueOf(sensorData.getHumidity().getValue()))
                .humidityUnit(sensorData.getHumidity().getUnit())
                .sulfurHValue(String.valueOf(sensorData.getSulfurH().getValue()))
                .sulfurHUnit(sensorData.getSulfurH().getUnit())
                .smokeValue(String.valueOf(sensorData.getSmoke().getValue()))
                .smokeUnit(sensorData.getSmoke().getUnit())
                .build();

        return this.save(record);
    }

    @Override
    public GasRecord getGasMonitor() {
        GasRecord one = this.getOne(Wrappers.lambdaQuery(GasRecord.class).orderByDesc(GasRecord::getCreateTime).last("limit 1"));
        GasRecord gasRecord = new GasRecord();
        gasRecord.setTemperatureValue(one.getTemperatureValue());
        gasRecord.setTemperatureUnit(one.getTemperatureUnit());
        gasRecord.setHumidityValue(one.getHumidityValue());
        gasRecord.setHumidityUnit(one.getHumidityUnit());
        gasRecord.setSulfurHValue(one.getSulfurHValue());
        gasRecord.setSulfurHUnit(one.getSulfurHUnit());
        gasRecord.setSmokeValue(one.getSmokeValue());
        gasRecord.setSmokeUnit(one.getSmokeUnit());
        return one;
    }
}